﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml;

using NUnit.Framework;

using VidoopSecure;
using VidoopSecureExtensionMethods;

namespace TestVidoopSecure
{
    [TestFixture]
    public class TestEnrollmentService : BaseTest
    {
        [TestFixtureSetUp]
        public override void SetUp()
        {
            base.SetUp();
        }

        [TestFixtureTearDown]
        public override void TearDown()
        {
            base.TearDown();
        }

        [Test]
        public void TestAPICall()
        {
            EnrollmentService service = (EnrollmentService)Manager.GetService(
               "enrollment", this.CurrentSite);

            List<Category> categories = service.GetCategories();

            // verify categories
            Assert.IsNotNull(categories);
            Assert.Greater(categories.Count, 0);
            Assert.IsNotNull(categories[0].Id);
            Assert.IsNotNull(categories[0].Name);
            Assert.Greater(categories[0].Images.Count, 0);
            Assert.IsNotNull(categories[0].Images[0].Uri);
            string imageUriBase = Settings.ApiBase + "/categories/" + categories[0].Id + "/representative_images/";

            Regex re = new Regex(Regex.Escape(imageUriBase));
            Assert.IsTrue(re.IsMatch(categories[0].Images[0].Uri));

            // verify that retrieval of a single category matches with above results
            string categoryId = categories[0].Id;
            Category firstCategory = service.GetCategory(categoryId);
            Assert.AreEqual(categories[0], firstCategory);

            // verify that retrieval of a single category's images matches with above results
            List<CategoryImage> images = service.GetCategoryImages(categoryId);
            Assert.IsNotNull(images);
            Assert.Greater(images.Count, 0);
            Assert.IsNotNull(images[0].Uri);
            Assert.AreEqual(categories[0].Images, images);

        }

        [Test]
        public void TestRandomBundle()
        {
            EnrollmentService service = (EnrollmentService)Manager.GetService(
               "enrollment", this.CurrentSite);

            // random bundle
            List<string> bundle = service.GetRandomBundle(6, null);
            Assert.IsNotNull(bundle);
            Assert.AreEqual(6, bundle.Count);

            // random bundle with specified categories
            List<string> parameters = new List<string>() { "outspac1", "airplns1" };
            bundle = service.GetRandomBundle(9, parameters);
            Assert.AreEqual(bundle.Count, 9);
            Assert.AreEqual("outspac1", bundle[0]);
            Assert.AreEqual("airplns1", bundle[1]);
        }

        [Test]
        public void TestImageResponseParsing()
        {
            FileStream stream = File.OpenRead(Settings.VidoopSecureTestData + "/image_response_1.xml");
            List<CategoryImage> images = CategoryImage.FromImagesXml(stream);
            Assert.AreEqual(1, images.Count);
            Assert.AreEqual("https://api.vidoop.com/vs/images/aLongRandomString/image", images[0].Uri);
        }

        [Test]
        public void TestCategoryResponseParsing() 
        { 
            FileStream stream = File.OpenRead(Settings.VidoopSecureTestData + "/category_response_1.xml");
            Category category = Category.FromXml(stream);
            Assert.IsNotNull(category);
            Assert.AreEqual("catid1", category.Id);
            Assert.AreEqual("category 1 name", category.Name);
            Assert.AreEqual(1, category.Images.Count);
            Assert.AreEqual(
                @"https://api.vidoop.com/vs/categories/catid1/representative_images/img123",
                category.Images[0].Uri);
        }

        [Test]
        public void TestCategoriesResponseParsing() 
        {
            FileStream stream = File.OpenRead(Settings.VidoopSecureTestData + "/categories_response_1.xml");
            List<Category> categories = Category.FromCategoriesXml(stream);
            Assert.AreEqual(2, categories.Count);

            Assert.AreEqual("catid1", categories[0].Id);
            Assert.AreEqual("category 1 name", categories[0].Name);
            Assert.AreEqual(1, categories[0].Images.Count);
            Assert.AreEqual(
                @"https://api.vidoop.com/vs/categories/catid1/representative_images/img123",
                categories[0].Images[0].Uri);

            Assert.AreEqual("catid2", categories[1].Id);
            Assert.AreEqual("category 2 name", categories[1].Name);
            Assert.AreEqual(2, categories[1].Images.Count);
            Assert.AreEqual(
                @"https://api.vidoop.com/vs/categories/catid2/representative_images/img123",
                categories[1].Images[0].Uri);
            Assert.AreEqual(
                @"https://api.vidoop.com/vs/categories/catid2/representative_images/img456",
                categories[1].Images[1].Uri);
        }
    }
}
